package gui;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JPasswordField;
import javax.swing.JButton;
import entities.Client;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.awt.Color;


@SuppressWarnings("serial")
public class MyAccount extends JFrame {

	//Fields
	private JPanel contentPane;
	private JLabel lblOldPassword;
	private JButton btnSave;
	private JTextField firstNameMytextField;
	private JTextField lastNameMytextField;
	private JTextField addressMyTextField;
	private JTextField mobileMyTextField;
	private JTextField emailMyTextField;
	private JPasswordField oldPasswordMyAccount;
	private JPasswordField newPasswordMyAccount;
	private JPasswordField confirmPasswordMyAccount;
	private JButton btnMyAccountConfirm;
	private JButton btnMyAccountCancel;
	private JButton btnMyAccountChangePassword;
	private JTextField userNameTextField;
	private String newPassword;
	private JLabel lblMyAccountPicture;

	/**
	 * Create the frame.
	 */
	public MyAccount(final Client client) {
		
		setTitle("My account");
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setBounds(250,30, 800, 600);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		newPassword = client.getUserPassword();

		
		final JLabel lblMobileNumber = new JLabel("Mobile Number");
		lblMobileNumber.setForeground(new Color(0, 0, 205));
		lblMobileNumber.setBounds(277, 142, 102, 16);
		contentPane.add(lblMobileNumber);
		
		
		final JLabel lblEmail = new JLabel("Email");
		lblEmail.setForeground(new Color(0, 0, 205));
		lblEmail.setBounds(309, 170, 38, 16);
		contentPane.add(lblEmail);
		
		final JLabel lblNewPassword = new JLabel("New password");
		lblNewPassword.setForeground(new Color(0, 0, 205));
		lblNewPassword.setBounds(277, 290, 91, 16);
		contentPane.add(lblNewPassword);
		lblNewPassword.setVisible(false);
		
		final JLabel lblConfirmPassword = new JLabel("Confirm password");
		lblConfirmPassword.setForeground(new Color(0, 0, 205));
		lblConfirmPassword.setBounds(261, 318, 119, 16);
		contentPane.add(lblConfirmPassword);
		lblConfirmPassword.setVisible(false);
		
		final JLabel lblFirstName = new JLabel("First name");
		lblFirstName.setForeground(new Color(0, 0, 205));
		lblFirstName.setBounds(288, 57, 74, 16);
		contentPane.add(lblFirstName);
		
		final JLabel lblLastName = new JLabel("Last name");
		lblLastName.setForeground(new Color(0, 0, 205));
		lblLastName.setBounds(290, 86, 72, 16);
		contentPane.add(lblLastName);
		
		final JLabel lblAddress = new JLabel("Address");
		lblAddress.setForeground(new Color(0, 0, 205));
		lblAddress.setBounds(300, 114, 61, 16);
		contentPane.add(lblAddress);
		
		
		lblOldPassword = new JLabel("Old password");
		lblOldPassword.setForeground(new Color(0, 0, 205));
		lblOldPassword.setBounds(277, 262, 91, 16);
		contentPane.add(lblOldPassword);
		lblOldPassword.setVisible(false);
		
		final JLabel lblUserName = new JLabel("User name");
		lblUserName.setForeground(new Color(0, 0, 205));
		lblUserName.setBounds(288, 29, 74, 16);
		contentPane.add(lblUserName);
		
		userNameTextField = new JTextField();
		userNameTextField.setForeground(new Color(0, 0, 205));
		userNameTextField.setEditable(false);
		userNameTextField.setBounds(394, 23, 134, 28);
		contentPane.add(userNameTextField);
		userNameTextField.setColumns(10);
		userNameTextField.setText(client.getUserName());
		
		firstNameMytextField = new JTextField();
		firstNameMytextField.setForeground(new Color(0, 0, 205));
		firstNameMytextField.setBounds(394, 51, 134, 28);
		contentPane.add(firstNameMytextField);
		firstNameMytextField.setColumns(10);
		firstNameMytextField.setText(client.getFirstName());
		
		lastNameMytextField = new JTextField();
		lastNameMytextField.setForeground(new Color(0, 0, 205));
		lastNameMytextField.setBounds(394, 80, 134, 28);
		contentPane.add(lastNameMytextField);
		lastNameMytextField.setColumns(10);
		lastNameMytextField.setText(client.getLastName());
		
		addressMyTextField = new JTextField();
		addressMyTextField.setForeground(new Color(0, 0, 205));
		addressMyTextField.setBounds(393, 108, 199, 28);
		contentPane.add(addressMyTextField);
		addressMyTextField.setColumns(10);
		addressMyTextField.setText(client.getAddress());
		
		mobileMyTextField = new JTextField();
		mobileMyTextField.setForeground(new Color(0, 0, 205));
		mobileMyTextField.setBounds(394, 136, 134, 28);
		contentPane.add(mobileMyTextField);
		mobileMyTextField.setColumns(10);
		mobileMyTextField.setText(client.getUserMobile());
		
		emailMyTextField = new JTextField();
		emailMyTextField.setForeground(new Color(0, 0, 205));
		emailMyTextField.setBounds(394, 164, 198, 28);
		contentPane.add(emailMyTextField);
		emailMyTextField.setColumns(10);
		emailMyTextField.setText(client.getUserEmail());
		
		oldPasswordMyAccount = new JPasswordField();
		oldPasswordMyAccount.setForeground(new Color(0, 0, 205));
		oldPasswordMyAccount.setBounds(394, 256, 134, 28);
		contentPane.add(oldPasswordMyAccount);
		oldPasswordMyAccount.setVisible(false);
		
		newPasswordMyAccount = new JPasswordField();
		newPasswordMyAccount.setForeground(new Color(0, 0, 205));
		newPasswordMyAccount.setBounds(394, 284, 134, 28);
		contentPane.add(newPasswordMyAccount);
		newPasswordMyAccount.setVisible(false);
		
		confirmPasswordMyAccount = new JPasswordField();
		confirmPasswordMyAccount.setForeground(new Color(0, 0, 205));
		confirmPasswordMyAccount.setBounds(394, 312, 134, 28);
		contentPane.add(confirmPasswordMyAccount);
		confirmPasswordMyAccount.setVisible(false);
		

		
		btnMyAccountConfirm = new JButton("Confirm");
		btnMyAccountConfirm.setForeground(new Color(0, 0, 205));
		btnMyAccountConfirm.addActionListener(new ActionListener() {
			@SuppressWarnings("deprecation")
			public void actionPerformed(ActionEvent e) {
				
				String newPassword = newPasswordMyAccount.getText();
				String oldPassword = oldPasswordMyAccount.getText();
				String confirmPassword = confirmPasswordMyAccount.getText();
				
				if(newPassword.isEmpty() || oldPassword.isEmpty() || confirmPassword.isEmpty() ){
					
					JOptionPane.showMessageDialog(null, "Some of the fields are EMPTY please fill it and try again");
				}
				else
					if(!oldPassword.equals(MyAccount.this.newPassword)){
	
						JOptionPane.showMessageDialog(null, "Inccorrect old Password ");
					}
					else
						if(newPassword.length()< 5 ){
							JOptionPane.showMessageDialog(null, "Password Must be at least 5 charchters");
						}
						else
							if(! newPassword.equals(confirmPassword)){
								JOptionPane.showMessageDialog(null, "New Password and Confirm Password Doesn't Match");
							}
							else{
								
								MyAccount.this.newPassword = newPasswordMyAccount.getText();
								client.setUserPassword(MyAccount.this.newPassword);
								btnMyAccountConfirm.setVisible(false);
								btnMyAccountCancel.setVisible(false);
								oldPasswordMyAccount.setVisible(false);
								newPasswordMyAccount.setVisible(false);
								confirmPasswordMyAccount.setVisible(false);
								lblOldPassword.setVisible(false);
								lblNewPassword.setVisible(false);
								lblConfirmPassword.setVisible(false);
								firstNameMytextField.setVisible(true);
								lastNameMytextField.setVisible(true);
								mobileMyTextField.setVisible(true);
								emailMyTextField.setVisible(true);
								addressMyTextField.setVisible(true);
								userNameTextField.setVisible(true);
								btnSave.setVisible(true);
								lblUserName.setVisible(true);
								lblAddress.setVisible(true);
								lblLastName.setVisible(true);
								lblFirstName.setVisible(true);
								lblEmail.setVisible(true);
								lblMobileNumber.setVisible(true);
								btnMyAccountChangePassword.setVisible(true);
								oldPasswordMyAccount.setText("");
								newPasswordMyAccount.setText("");
								confirmPasswordMyAccount.setText("");
							}
				}
		});
		btnMyAccountConfirm.setBounds(277, 367, 117, 29);
		contentPane.add(btnMyAccountConfirm);
		btnMyAccountConfirm.setVisible(false);
		
		btnSave = new JButton("Save");
		btnSave.setForeground(new Color(0, 0, 205));
		btnSave.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				
				String firstName = firstNameMytextField.getText();
				String lastName = lastNameMytextField.getText();
				String email = emailMyTextField.getText();
				String address = addressMyTextField.getText();
				String mobile = mobileMyTextField.getText();

			    Pattern pattern = Pattern.compile("\\d{3}\\d{7}");
			    Matcher matcher = pattern.matcher(mobile);
				
				if (firstName.isEmpty() || lastName.isEmpty()  || email.isEmpty()  || address.isEmpty()) 
					JOptionPane.showMessageDialog(null, "Some of the requierd fields are EMPTY please fill it !! ");
				else {
					String arr[] = email.split("@");
					if (arr.length != 2) 
						JOptionPane.showMessageDialog(null, "Incorrect Email please enter it again");
					else{
						String str[]= arr[1].split("\\.");
						if(str.length == 1)
							JOptionPane.showMessageDialog(null, "Incorrect Email please enter it again");
						else{
								if(!matcher.matches())
									JOptionPane.showMessageDialog(null, "Phone Number must be in the form XXXXXXXXXX");
								}
					}
				}
			}

		});
		
		btnSave.setBounds(328, 408, 83, 29);
		contentPane.add(btnSave);
	
		
		btnMyAccountCancel = new JButton("Cancel");
		btnMyAccountCancel.setForeground(new Color(0, 0, 205));
		btnMyAccountCancel.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				
				btnMyAccountConfirm.setVisible(false);
				btnMyAccountCancel.setVisible(false);
				oldPasswordMyAccount.setVisible(false);
				newPasswordMyAccount.setVisible(false);
				confirmPasswordMyAccount.setVisible(false);
				lblOldPassword.setVisible(false);
				lblNewPassword.setVisible(false);
				lblConfirmPassword.setVisible(false);
				lblUserName.setVisible(true);
				firstNameMytextField.setVisible(true);
				lastNameMytextField.setVisible(true);
				mobileMyTextField.setVisible(true);
				emailMyTextField.setVisible(true);
				addressMyTextField.setVisible(true);
				userNameTextField.setVisible(true);
				btnSave.setVisible(true);
				lblAddress.setVisible(true);
				lblLastName.setVisible(true);
				lblFirstName.setVisible(true);
				lblEmail.setVisible(true);
				lblMobileNumber.setVisible(true);
				btnMyAccountChangePassword.setVisible(true);
				
			}
		});
		btnMyAccountCancel.setBounds(406, 367, 117, 29);
		contentPane.add(btnMyAccountCancel);
		btnMyAccountCancel.setVisible(false);
		
		btnMyAccountChangePassword = new JButton("Change Password");
		btnMyAccountChangePassword.setForeground(new Color(0, 0, 205));
		btnMyAccountChangePassword.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				
				btnMyAccountConfirm.setVisible(true);
				btnMyAccountCancel.setVisible(true);
				oldPasswordMyAccount.setVisible(true);
				newPasswordMyAccount.setVisible(true);
				confirmPasswordMyAccount.setVisible(true);
				lblOldPassword.setVisible(true);
				lblNewPassword.setVisible(true);
				lblConfirmPassword.setVisible(true);
				firstNameMytextField.setVisible(false);
				lastNameMytextField.setVisible(false);
				mobileMyTextField.setVisible(false);
				emailMyTextField.setVisible(false);
				addressMyTextField.setVisible(false);
				userNameTextField.setVisible(false);
				btnSave.setVisible(false);
				lblUserName.setVisible(false);
				lblAddress.setVisible(false);
				lblLastName.setVisible(false);
				lblFirstName.setVisible(false);
				lblEmail.setVisible(false);
				lblMobileNumber.setVisible(false);
				btnMyAccountChangePassword.setVisible(false);
				

				
			}
		});
		btnMyAccountChangePassword.setBounds(277, 206, 134, 29);
		contentPane.add(btnMyAccountChangePassword);	
		
		lblMyAccountPicture = new JLabel("");
		lblMyAccountPicture.setIcon(new ImageIcon(MyAccount.class.getResource("/FramePictures/myaccount.jpg")));
		lblMyAccountPicture.setBounds(0, 0, 800, 578);
		contentPane.add(lblMyAccountPicture);
		

		
	}
	/*private Image ScaledImage(Image img , int width , int hight){
		
		BufferedImage resizedImage  = new BufferedImage(width, hight, BufferedImage.TYPE_INT_RGB);
		Graphics2D g2D = resizedImage.createGraphics();
		g2D.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
		g2D.drawImage(img, 0, 0, width, hight, null);
		g2D.dispose();
		return resizedImage;
	}*/
}
